#include <stdio.h>

static const int V = 1000;
int parent[V];

void init (int n)
{
	for (int i = 0; i < n; ++i)
		parent[i] = -1;
}

int root(int& v)
{
	int depth = 0;
	while (parent[v] != -1)
	{
		v = parent[v];
		++depth;
	}
	return depth;
}

bool merge(int& a, int& b)
{
	int d1 = root(a);
	int d2 = root(b);

	if (a == b)
		return false;

	if (d1 < d2)
		parent[a] = b;
	else
		parent[b] = a;

	return true;
}

int main()
{
	int n, m;
	while (1)
	{
		scanf ("%d", &n);
		if (n == 0)
			break;

		init (n);

		scanf ("%d", &m);
		while (m--)
		{
			int a, b;
			scanf ("%d %d", &a, &b);
			--a,--b;
			if (merge(a, b))
				--n;
		}
		printf ("%d\n", --n);
	}
	return 0;
}